<template>
  <div>
    <!-- 全局category下拉框 -->
    <Category :show="show"></Category>
    <!--  -->
    <el-card style="margin: 10px 0">
      <!-- 场景0 -->
      <div v-show="show === 0">
        <!-- 添加SPU按钮 -->
        <!-- disabled:如果c3Id有值才可以点击  -->
        <el-button
          type="primary"
          :icon="Plus"
          :disabled="categoryStore.c3Id ? false : true"
          @click="addSpu"
          >添加SPU</el-button
        >
        <!-- table表格 -->
        <el-table border style="margin: 10px 0" :data="data">
          <el-table-column
            label="序号"
            width="80"
            align="center"
            type="index"
          ></el-table-column>
          <el-table-column label="SPU名称" property="spuName"></el-table-column>
          <el-table-column
            label="SPU描述"
            prop="description"
            :show-overflow-tooltip="true"
          ></el-table-column>
          <el-table-column label="操作">
            <!-- 当前row为一个已知的spu对象 -->
            <template #="{ row, index }">
              <el-button
                :icon="Plus"
                size="small"
                type="primary"
                title="添加sku"
                @click="addsku(row)"
              ></el-button>
              <el-button
                :icon="Edit"
                size="small"
                type="warning"
                title="修改spu"
                @click="updateSpu(row)"
              ></el-button>
              <el-button
                :icon="Pointer"
                size="small"
                type="info"
                title="查看sku列表"
                @click="lookSku(row.id)"
              ></el-button>

              <el-popconfirm  :title="`你确定删除${row.spuName}?`" @confirm="removeSku(row.id)">
                <template #reference>
                  <el-button
                    :icon="Delete"
                    size="small"
                    type="danger"
                    title="删除spu"
                  ></el-button>
                </template>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页器 -->
        <el-pagination
          v-model:current-page="current"
          v-model:page-size="pageSize"
          :page-sizes="[3, 5, 7, 9]"
          :background="true"
          layout=" prev, pager, next, jumper,->, sizes,total"
          :total="total"
          @current-change="getData"
          @size-change="pageSizeChange"
        />
      </div>
      <!-- 场景1 -->
      <SpuForm
        v-show="show === 1"
        @changeShowOne="changeShowOne"
        ref="spu"
      ></SpuForm>
      <!-- 场景2 -->
      <SkuForm v-show="show === 2" @changeSku="changeSku" ref="sku"></SkuForm>
      <!-- dialog -->
      <el-dialog title="sku列表" v-model="dialog" center>
        <el-table border :data="skuData">
          <el-table-column label="sku名称" prop="skuName"></el-table-column>
          <el-table-column label="sku价格" prop="price"></el-table-column>
          <el-table-column label="sku重量" prop="weight"></el-table-column>
          <el-table-column label="sku图片">
            <template #="{ row, $index }">
              <img
                :src="row.skuDefaultImg"
                style="width: 100px; height: 100px"
              />
            </template>
          </el-table-column>
        </el-table>
      </el-dialog>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { Plus, Delete, Pointer, Edit } from "@element-plus/icons-vue";
import { ref, onUnmounted, watch } from "vue";
import SkuForm from "./SkuForm.vue";
import SpuForm from "./SpuForm.vue";
//请求
import { reqSpuInfo, reqLookSku, reqRemoveSpu } from "@/api/product/spu/index";
//仓库
import useCategoryStore from "@/stores/category";
import { ElMessage } from "element-plus";

//获取小仓库
let categoryStore = useCategoryStore();

//监听c3id
watch(
  () => categoryStore.c3Id,
  () => {
    // c3Id发生变化,先清空数据
    data.value = [];
    // 判断当前为位空字符串不请求
    if (categoryStore.c3Id == "") return;
    getData();
  }
);
// 场景状态
let show = ref(0);
// 分页器当前页码
let current = ref(1);
// 分页器当前页显示数量
let pageSize = ref<number>(5);
// 定义总数据
let total = ref(0);
// 定义已知的spu列表
let data = ref([]);
// 获取子组件spu的vc
let spu = ref();
//获取子组件sku的vc
let sku = ref();
// 定义dialog状态
let dialog = ref(false);
//接收sku列表数据
let skuData = ref([]);

// 分页器下拉菜单发生变化
let pageSizeChange = () => {
  // 当前页码归1
  current.value = 1;
  // 调用请求
  getData();
};

//添加spu按钮的回调
let addSpu = () => {
  //切换场景
  show.value = 1;
  //调用子组件方法
  // 传c3id
  spu.value.addSpu(categoryStore.c3Id);
};
//修改spu按钮的回调
let updateSpu = (row: any) => {
  show.value = 1;
  // 调用子组件方法,发送请求
  spu.value.getData(row);
};
//添加sku按钮的回调
let addsku = (row: any) => {
  show.value = 2;
  //调用子组件方法
  sku.value.addsku(categoryStore.c1Id, categoryStore.c2Id, row);
};
// spuForm自定义事件的回调:子组件给父组件值,切换场景0
let changeShowOne = ({ newScene, falg }) => {
  show.value = newScene;
  // //重新渲染
  // 如果falg是空字符串没有值 代表是新增,回第一页
  if (!falg) current.value = 1;
  // 重新渲染
  getData();
};
// sku自定义事件的回调,切换场景
let changeSku = (val: number) => {
  show.value = val;
};
//查看sku按钮的回调
let lookSku = async (spuId: number) => {
  dialog.value = true;
  //发请求
  let result: any = await reqLookSku(spuId);
  skuData.value = result;
};
//删除sku回调
let removeSku = async (spuId: number)=>{
    try{
      await reqRemoveSpu(spuId)
      ElMessage({
        message:'删除成功',
        type:'success'
      })
      // 发请求
      getData()
    }catch(e){
      ElMessage({
        message:'删失败 ',
        type:'error'
      })
    }
}

// 封装请求列表
let getData = async () => {
  //整理参数
  let result: any = await reqSpuInfo(
    current.value,
    pageSize.value,
    categoryStore.c3Id
  );
  data.value = result.records;
  total.value = result.total;
};
//卸载
onUnmounted(() => {
  //组件卸载,将数据清空
  categoryStore.c1Arr = [];
  categoryStore.c2Arr = [];
  categoryStore.c3Arr = [];
  categoryStore.c1Id = "";
  categoryStore.c2Id = "";
  categoryStore.c3Id = "";
});
</script>
